package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.zaofan.cache.CacheManage;
import com.zaofan.constant.LableConstant;
import com.zaofan.constant.UrlConstant;
import com.zaofan.mobile.bean.SpecialShop;
import com.zaofan.mobile.bean.SpecialShop1;
import com.zaofan.service.ShopService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/index")
public class IndexController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;

	private ShopService shopService = new ShopService();

	/**
	 * app 首页接口 包含全部数据接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void home(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("banner", CacheManage.banner);
			data.put("centerAd", CacheManage.centerAd);
			data.put("fourAd", CacheManage.fourAd);
			List<SpecialShop> shops = CacheManage.specialShop;
			Calendar now = Calendar.getInstance();
			Calendar beginTime = Calendar.getInstance();
			Calendar endTime = Calendar.getInstance();
			data.put("special", shops);
			String builderId = request.getParameter("builderId");
			List<com.zaofan.bean.Shop> shops2 = shopService
					.findByBuilderIdWithPage(builderId, "0", "10");
			List<com.zaofan.mobile.bean.Shop> target = new ArrayList<>();
			com.zaofan.mobile.bean.Shop s = null;
			for (com.zaofan.bean.Shop shop : shops2) {
				s = new com.zaofan.mobile.bean.Shop();
				BeanUtils.copyProperties(s, shop);
				beginTime.set(Calendar.HOUR_OF_DAY,
						Integer.valueOf(shop.getBeginTime().split(":")[0]));
				beginTime.set(Calendar.MINUTE,
						Integer.valueOf(shop.getBeginTime().split(":")[1]));
				beginTime.set(Calendar.SECOND,
						Integer.valueOf(shop.getBeginTime().split(":")[2]));
				endTime.set(Calendar.HOUR_OF_DAY,
						Integer.valueOf(shop.getEndTime().split(":")[0]));
				endTime.set(Calendar.MINUTE,
						Integer.valueOf(shop.getEndTime().split(":")[1]));
				endTime.set(Calendar.SECOND,
						Integer.valueOf(shop.getEndTime().split(":")[2]));
				if (now.before(beginTime) || now.after(endTime)) {
					s.setIsTimeOut(0);
					if (shop.getTimeoutImage() != null
							&& !"".equals(shop.getTimeoutImage())) {
						s.setShopImage(UrlConstant.HOST_URL
								+ shop.getTimeoutImage());
					}
				} else {
					s.setIsTimeOut(1);
				}
				target.add(s);
			}
			data.put("shop", target);
			JsonUtils.writeSuccessJson("查询成功", data, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * app 首页 轮播接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void banner(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			JsonUtils.writeSuccessJson("查询成功", CacheManage.banner, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * app 首页 全城配送接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void special(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			JsonUtils.writeSuccessJson("查询成功", CacheManage.specialShop,
					response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * app 首页 四张广告位接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void fourAd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			JsonUtils.writeSuccessJson("查询成功", CacheManage.fourAd, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * app 首页 中部广告位
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void centerAd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			JsonUtils.writeSuccessJson("查询成功", CacheManage.centerAd, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * app 首页 商家分类接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shopLable(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			JsonUtils.writeSuccessJson("查询成功",
					CacheManage.lables.get(LableConstant.lable_shop_type),
					response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	public void activityAd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			if (CacheManage.activityAd != null) {
				JsonUtils.writeSuccessJson("查询成功", CacheManage.activityAd,
						response);
			} else {
				JsonUtils.writeBeanFailJson("暂无广告", response);
			}
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}



}
